package com.xiaobai.netease_news.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.model.HotPic;
import com.xiaobai.netease_news.model.Photo;
import com.xiaobai.netease_news.utils.CommonHttpUtil;
import com.xiaobai.netease_news.utils.ImageUniversalLoaderConfiguration;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class PicActivity extends Activity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private List<ImageView> imageViewList;
    private TextView picTitle;
    private TextView picDesc;
    private DisplayImageOptions options;
    private List<String> notes;
    private ViewPager picPager;
    private TextView picCount;
    private String imgsum;
    private MyPicAdapter adapter;
    private ProgressBar picProgress;
    private RelativeLayout picHead;
    private RelativeLayout picText;
    private boolean isShowing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic);

        options = ImageUniversalLoaderConfiguration.initOptions();

        picHead = (RelativeLayout) findViewById(R.id.pic_head);
        picText = (RelativeLayout) findViewById(R.id.pic_text);

        Intent intent = getIntent();
        int replycount = intent.getIntExtra("replyCount", 0);
        String photosetId = intent.getStringExtra("photosetId");

        String picsUrl = "http://c.3g.163.com/photo/api/set/" + photosetId + ".json";

        picPager = (ViewPager) findViewById(R.id.pic_pager);
        adapter = new MyPicAdapter();
        picPager.setAdapter(adapter);
        picPager.setOnPageChangeListener(this);
        imageViewList = new ArrayList<>();
        notes = new ArrayList<>();

        picTitle = (TextView) findViewById(R.id.pic_title);
        picDesc = (TextView) findViewById(R.id.pic_desc);
        TextView replyCount = (TextView) findViewById(R.id.pic_reply_count);
        replyCount.setText(String.valueOf(replycount) + "跟帖");
        picCount = (TextView) findViewById(R.id.pic_count);

        picProgress = (ProgressBar) findViewById(R.id.pic_progress);
        picProgress.setVisibility(View.VISIBLE);

        new PicTask().execute(picsUrl);

        ImageView picBack = (ImageView) findViewById(R.id.pic_back);
        picBack.setOnClickListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        picDesc.setText(notes.get(position));
        picCount.setText((position + 1) + "/" + imgsum);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pic_back:
                finish();
                break;
        }
    }

    private class MyPicAdapter extends PagerAdapter implements View.OnClickListener {

        @Override
        public int getCount() {
            if (imageViewList != null) {
                return imageViewList.size();
            }
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            picPager.addView(imageViewList.get(position));

            imageViewList.get(position).setOnClickListener(this);

            return imageViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (object instanceof ImageView)
                container.removeView((View) object);
        }

        @Override
        public void onClick(View v) {
            if (isShowing) {
                picHead.setVisibility(View.GONE);
                picText.setVisibility(View.GONE);
                isShowing = false;
            } else {
                picHead.setVisibility(View.VISIBLE);
                picText.setVisibility(View.VISIBLE);
                isShowing = true;
            }
        }
    }

    private class PicTask extends AsyncTask<String, Void, JSONObject> {

        @Override
        protected JSONObject doInBackground(String... params) {

            String url = params[0];

            byte[] bytes = CommonHttpUtil.doGet(url);
            if (bytes != null) {
                try {
                    String jsonString = new String(bytes, "utf-8");
                    return new JSONObject(jsonString);
                } catch (UnsupportedEncodingException | JSONException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {

            picProgress.setVisibility(View.GONE);

            if (jsonObject != null) {
                HotPic hotPic = new HotPic();
                hotPic.parseJson(jsonObject);

                String setname = hotPic.getSetname();
                picTitle.setText(setname);

                imgsum = hotPic.getImgsum();
                picCount.setText("1/" + imgsum);

                List<String> imgurls = new ArrayList<>();
                List<Photo> photos = hotPic.getPhotos();
                for (Photo photo : photos) {
                    String note = photo.getNote();
                    notes.add(note);
                    String imgurl = photo.getImgurl();
                    imgurls.add(imgurl);
                }

                ImageView imageView;
                for (int i = 0; i < imgurls.size(); i++) {
                    imageView = new ImageView(PicActivity.this);
                    imageView.setLayoutParams(new ViewGroup.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));
                    ImageLoader.getInstance().displayImage(imgurls.get(i), imageView, options);
                    imageViewList.add(imageView);
                }

                adapter.notifyDataSetChanged();
            }
        }
    }
}
